import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:() => import('../components/Home.vue'),
    redirect:'/messages',
    children:[{
      path:'/welcome',
      component:() => import('../components/index/welcome.vue')
    },
    {
      path:'/users',
      component:() => import('../components/users/users.vue')
    },
    {
      path:'/rights',
      component:() => import('../components/power/Rights.vue')
    }, 
    {
      path:'/roles',
      component:() => import('../components/power/Roles.vue')
    },   
    {
      path:'/messages',
      component:() => import('../components/messages/messages.vue')
    },
    {
      path:'/urls',
      component:() => import('../components/check/check.vue')
    },    
    {
      path:'/categories',
      component:() => import('../components/goods/Cate.vue')
    },
    {
      path:'/params',
      component:() => import('../components/goods/Params.vue')
    },
    {
      path:'/goods',
      component:() => import('../components/goods/List.vue')
    },
    {
      path:'/goods/add',
      component:() => import('../components/goods/Add.vue')
    },
    {
      path:'/goods/edit',
      component:() => import('../components/goods/Edit.vue')
    },
    {
      path:'/goodsproduct',
      component:() => import('../components/goodsproduct/goodsproduct.vue')
    },
    {
      path:'/orders',
      component:() => import('../components/order/orders.vue')
    },
    {
      path:'/hotsearch',
      component:() => import('../components/hotsearch/hotsearch.vue')
    }, 
    {
      path:'/storeservice',
      component:() => import('../components/storeservice/storeservice.vue')
    }, 
    {
      path:'/yy',
      component:() => import('../components/yy1170/yy1170.vue')
    },
    {
      path:'/yy/edit',
      component:() => import('../components/yy1170/Edit.vue')
    }, 
    {
      path:'/report',
      component:() => import('../components/report1170/report1170.vue')
    },    
    {
      path:'/ad',
      component:() => import('../components/ad/ad.vue')
    }, 
    {
      path:'/mallmode',
      component:() => import('../components/mallmode/mallmode.vue')
    }, 
    {
      path:'/care',
      component:() => import('../components/care/care.vue')
    }, 
    {
      path:'/ias',
      component:() => import('../components/ias/ias.vue')
    },    
  ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  if(to.path === '/login'){
    return next()
  }
  console.log('userInfo index.js', sessionStorage.getItem('userInfo'))
  const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
  if(!userInfo){
    return next('/login')
  }
  next()
})

export default router
